/**
 * 
 */
package com.simtek.simplayer.controller;

import java.io.File;
import java.util.Date;

import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.text.format.DateFormat;
import android.util.Log;

import com.simtek.simplayer.utils.OsDetectUtils;
import com.simtek.simplayer.utils.StringUtils;

/**
 * @author Hitech
 * 
 */
public class VideoDownloadController {
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	public static void downloadVideoUrl(Context context, String url, String fileName, String fileExtention) {
		if (StringUtils.isBlank(fileName)) {
			fileName = "clip-" + DateFormat.format("yyyy-MM-dd hh:mm:ss", new Date());
		}
		Log.d("down", url + " " + fileName + " " + fileExtention);
		

		Request request = new Request(Uri.parse(url));
		request.setTitle(fileName);
		request.setVisibleInDownloadsUi(true);
		request.setAllowedNetworkTypes(Request.NETWORK_WIFI);

		if (OsDetectUtils.hasJellyBean()) {
			request.setAllowedOverMetered(true);
		}

		File saveFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName + "." + fileExtention);
		request.setDestinationUri(Uri.fromFile(saveFile));

		DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
		dm.enqueue(request);
	}
}
